<template>
  <div>
    <a-card :bordered="false">
      <f-search-form @searchHandler="searchHandler">
        <f-search-form-item label="专业包代码" name="majorCode" slot="advanced"/>
        <f-search-form-item label="专业包名称" name="majorName"/>
        <f-search-form-item label="工程包代码" name="packageNo" slot="advanced"/>
        <f-search-form-item label="工程包名称" name="packageName"/>
      </f-search-form>
      <f-table ref="table" rowKey="materialPackage100Id" :scroll="{x:columns.length*150}" :columns="columns"
               :exportBtn="false"
               :multiple="false" :operator="operator" :params="queryParam" :api="api.package"
               handler="materialPackagePageListHandler">
         <span slot="action" slot-scope="text, record, index">
          <template>
            <!-- <a @click="$refs.reportDrawer.showDrawer(record)">填报</a>
             <a-divider type="vertical"/>-->
            <a @click="$refs.reportFormModal.handleShow(record)">填报</a>
             <a-divider type="vertical"/>
             <a @click="$refs.reportDetailsDrawer.showDrawer(record)">查看月报</a>
             <a-divider type="vertical"/>
          </template>
        </span>
      </f-table>
      <report-drawer ref="reportDrawer"></report-drawer>
      <report-form-modal ref="reportFormModal"></report-form-modal>
      <report-details-drawer ref="reportDetailsDrawer" :listType="1"></report-details-drawer>
      <previewForSrm ref="preview" @previewTab="previewTab"></previewForSrm>
      <upload-report-modal ref="uploadReportModal" :action="api.report.importBusReport"
                           @change="uploadEnd()"></upload-report-modal>
    </a-card>
  </div>
</template>
<script>
  import api from './js/apiPackage'
  import apiReport from './js/apiReport'
  import reportDrawer from './report_drawer'
  import reportFormModal from './report_form_modal'
  import reportDetailsDrawer from './report_details_drawer'
  import uploadReportModal from './uploadReportModal'
  import previewForSrm from './preview_for_srm'

  export default {
    name: 'TableList',
    props: {
      detailsParams: {}
    },
    components: {
      reportDetailsDrawer,
      reportDrawer,
      reportFormModal,
      uploadReportModal,
      previewForSrm
    },
    data() {
      let me = this
      //操作按钮
      let operator = []
      operator.push({
        name: '下载月报模板',
        icon: '',
        that: this,
        handler: function(table, that) {
          let columns = table.getExportColumns(that.columns)
          columns.push({ name: '申报数量', isStatistics: '0', key: 'declareQuantity' })
          columns.push({ name: '确认数量', isStatistics: '0', key: 'confirmQuantity' })
          columns.push({ name: '填报月份', isStatistics: '0', key: 'reportMonth' })
          that.detailsParams['columns'] = JSON.stringify(columns)
          that.downloadTemplate()
        }
      })
      operator.push({
        name: '导入月报',
        icon: '',
        that: this,
        handler: function(table, that) {
          that.showImportData()
        }
      })
      operator.push({
        name: '月报下达',
        icon: '',
        that: this,
        handler: function(table, that) {
          that.$refs['preview'].handleShow(that.detailsParams)
        }
      })
      return {
        api: Object.assign(api, apiReport, this.api),
        //列表初始化查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '序号',
            width: 80,
            align: 'center',
            customRender: (text, record, index) => index + 1
          },
          {
            title: '分包Id',
            width: 180,
            align: 'center',
            dataIndex: 'materialPackage100Id'
          },
          {
            title: '版本Id',
            width: 180,
            align: 'center',
            dataIndex: 'versionId'
          },
          {
            title: '项目代码',
            width: 150,
            align: 'center',
            dataIndex: 'projectCode'
          },
          {
            title: '项目名称',
            width: 150,
            align: 'center',
            dataIndex: 'projectName'
          },
          {
            title: '专业名称',
            width: 150,
            align: 'center',
            dataIndex: 'majorName'
          },
          {
            title: '专业代码',
            width: 150,
            align: 'center',
            dataIndex: 'majorCode'
          },
          {
            title: '工程包名称',
            width: 150,
            align: 'center',
            dataIndex: 'packageName'
          },
          {
            title: '工程包代码',
            width: 150,
            align: 'center',
            dataIndex: 'packageNo'
          },
          {
            title: '服务代码',
            width: 150,
            align: 'center',
            dataIndex: 'serviceCode'
          },
          {
            title: '服务WBS代码',
            width: 150,
            align: 'center',
            dataIndex: 'serviceWbsCode'
          },
          {
            title: '科目名称',
            width: 150,
            align: 'center',
            dataIndex: 'subjectName'
          },
          {
            title: '层级号',
            width: 150,
            align: 'center',
            dataIndex: 'levelNo',
            export: true
          },
          {
            title: '分承包发包项目描述',
            width: 150,
            align: 'center',
            dataIndex: 'packageDesc'
          },
          {
            title: '对外合同描述',
            width: 150,
            align: 'center',
            dataIndex: 'contractDesc'
          },
          {
            title: '区域',
            width: 150,
            align: 'center',
            dataIndex: 'materialArea'
          },
          {
            title: '系统',
            width: 150,
            align: 'center',
            dataIndex: 'materialUse'
          },
          {
            title: '计量单位',
            width: 150,
            align: 'center',
            dataIndex: 'measurementUnit'
          },
          {
            title: '合同清单工程量',
            width: 150,
            align: 'center',
            dataIndex: 'contractBillQuantity'
          },
          {
            title: '标前工程量',
            width: 150,
            align: 'center',
            dataIndex: 'beforeMarkQuantity'
          },
          {
            title: '100预算成本工程量',
            width: 200,
            align: 'center',
            dataIndex: 'costQuantity100'
          },
          {
            title: '200预算成本工程量',
            width: 200,
            align: 'center',
            dataIndex: 'costQuantity200'
          }
        ],
        operator: operator
      }
    },
    created() {
      this.detailsParams.isReport = true
      this.queryParam = this.detailsParams
      this.getBusProjectSupplierList()
    },
    methods: {
      previewTab() {
        this.$refs.table.refresh()
      },
      downloadTemplate() {
        let that = this
        that.http.exportExcel(that, {
          url: that.api.package.exportTemplate,
          data: that.detailsParams
        })
      },
      showImportData() {
        let params = { ...this.detailsParams }
        params.materialPackage100Id = params.materialId
        this.$refs.uploadReportModal.open(params)
      },
      uploadEnd() {
        this.$refs.uploadReportModal.close()
        this.$message.success('导入成功')
        this.$refs.table.refresh(false, this.queryParam)
      },
      getBusProjectSupplierList() {
        let params = { ...this.detailsParams }
        params.materialPackage100Id = params.materialId
        this.http.get(this, {
          url: this.api.package.getBusProjectSupplierList,
          data: params,
          success(me, data) {
            console.log(data)
            for (let i in data) {
              let info = data[i]
              me.columns.push({
                title: info.supplierName + '合同量',
                width: 250,
                align: 'center',
                dataIndex: 'supplierNum' + info.index
              })
            }
            me.columns.push({
              title: '操作',
              width: 150,
              align: 'center',
              scopedSlots: { customRender: 'action' },
              fixed: 'right'
            })
          }
        })
      }
    }
  }
</script>
